package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPickerView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.Calendar;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddWorkerActivity extends AppCompatActivity {

    private EditText add_worker_TeaName;
    private RadioButton add_worker_Teasex1;
    private RadioButton add_worker_Teasex2;
    private EditText add_worker_TeaAge;
    private EditText add_worker_Teacarid;
    private EditText add_worker_TeaPhone;
    private EditText add_worker_TeaEmail;
    private TextView add_worker_TeaWorkerLocal;
    private TextView add_worker_TeabackTime;
    private Spinner add_worker_PositionId;

    private Button add_worker_Tea_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_worker);
        initid();
        add_worker_Tea_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tijiao();
            }
        });
        add_worker_TeabackTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog(add_worker_TeabackTime);
            }
        });
        add_worker_TeaWorkerLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CityPickerView cityPickerView = new CityPickerView(AddWorkerActivity.this);
                cityPickerView.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        //省份
                        String province = citySelected[0];
                        //城市
                        String city = citySelected[1];
                        //区县
                        String district = citySelected[2];
                        //邮编
                        String code = citySelected[3];
                        //控件自己写两个edittext或者Xedittext用来显示文本代替下面两个个控件ID
                        //address_new_address用来展示地址
                        //address_new_zipcode用来展示邮编
                        add_worker_TeaWorkerLocal.setText(province+"-"+ city+"-"+ district);
                    }
                });
                cityPickerView.show();

            }
        });
    }

    private void initid(){

        add_worker_TeaName = (EditText) findViewById(R.id.add_worker_TeaName);
        add_worker_Teasex1 = (RadioButton) findViewById(R.id.add_worker_Teasex1);
        add_worker_Teasex2 = (RadioButton) findViewById(R.id.add_worker_Teasex2);
        add_worker_TeaAge = (EditText) findViewById(R.id.add_worker_TeaAge);
        add_worker_Teacarid = (EditText) findViewById(R.id.add_worker_Teacarid);
        add_worker_TeaPhone = (EditText) findViewById(R.id.add_worker_TeaPhone);
        add_worker_TeaEmail = (EditText) findViewById(R.id.add_worker_TeaEmail);
        add_worker_TeaWorkerLocal = (TextView) findViewById(R.id.add_worker_TeaWorkerLocal);
        add_worker_TeabackTime = (TextView) findViewById(R.id.add_worker_TeabackTime);

        add_worker_PositionId = (Spinner) findViewById(R.id.add_worker_PositionId);

        add_worker_Tea_btn = (Button) findViewById(R.id.add_worker_Tea_btn);
    }

    private void tijiao(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("Id",0);
                    jsonObject.put("WorkerId","9001");
                    jsonObject.put("WorkerName",add_worker_TeaName.getText().toString());
                    jsonObject.put("WorkerAge",Integer.parseInt(add_worker_TeaAge.getText().toString()));
                    String sex;
                    if (add_worker_Teasex1.isChecked()){
                        sex = "男";
                    }else {
                        sex = "女";
                    }
                    jsonObject.put("WorkerSex",sex);
                    jsonObject.put("WorkerCarid",add_worker_Teacarid.getText().toString());
                    jsonObject.put("WorkerPhone",add_worker_TeaPhone.getText().toString());
                    int PositionId;
                    if (add_worker_PositionId.getSelectedItemId()==0){
                        PositionId=3;
                    }else{
                        PositionId=6;
                    }
                    jsonObject.put("PositionId",PositionId);
                    jsonObject.put("WorkerLocal",add_worker_TeaWorkerLocal.getText().toString());
                    jsonObject.put("WorkerbackTime",add_worker_TeabackTime.getText().toString());
                    jsonObject.put("WorkerEmail",add_worker_TeaEmail.getText().toString());


                    Log.d("数据",jsonObject.toString());
                    MediaType mediaType = MediaType.parse("application/json-patch+json");
                    RequestBody requestBody = RequestBody.create(mediaType,""+jsonObject.toString());
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/Worker/InsertWorker")
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request1).execute();
                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String code = jsonObject1.getString("code");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (code.equals("200")){
                                Toast.makeText(AddWorkerActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(AddWorkerActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //弹出选择日期的对话框
    private void showDatePickerDialog(final TextView textView){
        Calendar c = Calendar.getInstance();
        new DatePickerDialog(AddWorkerActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                textView.setText(i+"-"+(i1+1)+"-"+i2);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
    }

}